
using System;
using Serpis;

namespace PVenta
{


	public partial class ArticuloView : Gtk.Window, IEntityView
	{
		public event EventHandler Changed;

		public ArticuloView () : base(Gtk.WindowType.Toplevel)
		{
			this.Build ();
			
			precio.Adjustment.Lower= int.MinValue;
			precio.Adjustment.Upper= int.MaxValue;
			precio.Digits= 2;
			
			categoria.Init(AppDbConnection.Instance.SessionFactory, typeof(Categoria));
			
			nombre.Changed += delegate {
				raiseChanged();
			};
		}
		
		[ModelProperty]
		public string Nombre {
			get {return nombre.Text;}
			set {nombre.Text = value ?? "";}
		}
		
		[ModelProperty]
		public decimal Precio {
			get {return Convert.ToDecimal(precio.Value);}
			set {precio.Value = Convert.ToDouble(value);}
		}
		
		[ModelProperty]
		public object Categoria {
			get {return ((Categoria)categoria.CurrentItem).Id == 0 ? null : categoria.CurrentItem;}
			set {categoria.CurrentItem = value;}
		}
		
		public Gtk.Action SaveAction {
			get {return saveAction;}
		}
		
		private void raiseChanged() {
			if(Changed != null)
				Changed(this, EventArgs.Empty);
		}
	}
}